import ACCESS_ENUM from "@/access/ACCESS_ENUM";
import { LoginUserVO } from "@/api";

/**
 * 检查权限(判断当前用户是否具有某个权限)
 * @param currentUser
 * @param needAccess
 */
const checkAccess = (
  currentUser: LoginUserVO,
  needAccess = ACCESS_ENUM.NO_LOGIN
) => {
  //获取当前用户具有的权限,空则表示未登录
  const userAccess = currentUser?.role ?? ACCESS_ENUM.NO_LOGIN;

  //允许未登录用户访问页面,放行
  if (needAccess === ACCESS_ENUM.NO_LOGIN) {
    return true;
  }
  //只允许登录访问
  if (needAccess === ACCESS_ENUM.USER) {
    if (userAccess === ACCESS_ENUM.NO_LOGIN) {
      return false;
    }
  }
  //只允许管理员访问
  if (needAccess === ACCESS_ENUM.ADMIN) {
    if (userAccess !== ACCESS_ENUM.ADMIN) {
      return false;
    }
  }
  return true;
};

export default checkAccess;
